package utils

import (
	"flag"
	"fmt"
	"k8s.io/client-go/discovery"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
	"path/filepath"
)

package main

import (
"context"
"flag"
"fmt"
"path/filepath"

"k8s.io/client-go/discovery"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)

func getInfos() {
	var kubeconfig *string
	if home := homedir.HomeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "kubeconfig file")
	}
	flag.Parse()

	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err)
	}

	discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
	if err != nil {
		panic(err)
	}

	apiGroupList, err := discoveryClient.ServerGroups()
	if err != nil {
		panic(err)
	}

	fmt.Println("API Groups supported by the cluster:")
	for _, group := range apiGroupList.Groups {
		fmt.Printf("- %s (versions: ", group.Name)
		for i, version := range group.Versions {
			if i > 0 {
				fmt.Print(", ")
			}
			fmt.Print(version.Version)
		}
		fmt.Println(")")
	}

	// 列出 core/v1 资源
	resourceList, err := discoveryClient.ServerResourcesForGroupVersion("v1")
	if err != nil {
		panic(err)
	}

	fmt.Printf("\nResources in core/v1:\n")
	for _, resource := range resourceList.APIResources {
		fmt.Printf("- %s (namespaced: %v)\n", resource.Name, resource.Namespaced)
	}
}
